import { Task } from './../../../model/task';
import { TaskApiService } from './../../../api/task-api.service';
import { Overview } from './../../../model/statitistics';
import { StatisticsService } from './../../../api/statistics.service';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-today',
  templateUrl: './today.component.html',
  styleUrls: ['./today.component.less'],
  providers: [StatisticsService]
})
export class TodayComponent implements OnInit {


  tasksEndInToday = new Array<Task>();
  tasksBeginInToday = new Array<Task>();
  tasksEndInTomorrow = new Array<Task>();
  tasksEndInDayAfterTomorrow = new Array<Task>();
  overview = new Overview();
  constructor(
    private statisticsApi: StatisticsService,
    private taskApi: TaskApiService,
  ) { }

  ngOnInit(): void {
    this.fetchOverview();
    this.fetchTaskEndInToday();
    this.fetchTaskBeginInToday();
    this.fetchTaskEndInTomorrow();
    this.fetchTaskDayAfterTomorrow();
  }


  private fetchOverview(): void {
    this.statisticsApi.fetchOverview(new Date()).subscribe(
      response => {
        this.overview = response;
      },
      err => { }
    );
  }

  endListChange(): void {
    this.fetchTaskEndInToday();
  }
  beginListChange(): void {
    this.fetchTaskBeginInToday();
  }
  tomorrowListChange(): void {
    this.fetchTaskEndInTomorrow();
  }
  dayAfterTomorrowListChange(): void {
    this.fetchTaskDayAfterTomorrow();
   }

  private fetchTaskEndInToday(): void {
    const beginDate = new Date();
    const endDate = new Date(beginDate.getTime() + 24 * 60 * 60 * 1000);
    this.taskApi.listEndBetween(beginDate, endDate).subscribe(
      response => {
        this.tasksEndInToday = response.map(param => new Task(param));
      },
      err => { }
    );
  }

  private fetchTaskBeginInToday(): void {
    const beginDate = new Date();
    const endDate = new Date(beginDate.getTime() + 24 * 60 * 60 * 1000);
    this.taskApi.listBeginBetween(beginDate, endDate).subscribe(
      response => {
        this.tasksBeginInToday = response.map(param => new Task(param));
      },
      err => { }
    );
  }

  private fetchTaskEndInTomorrow(): void {
    const beginDate = new Date(Date.now() + 24 * 60 * 60 * 1000);
    const endDate = new Date(beginDate.getTime() + 24 * 60 * 60 * 1000);
    this.taskApi.listEndBetween(beginDate, endDate).subscribe(
      response => {
        this.tasksEndInTomorrow = response.map(param => new Task(param));
      },
      err => { }
    );
  }

  private fetchTaskDayAfterTomorrow(): void {
    const beginDate = new Date(Date.now() + 2 * 24 * 60 * 60 * 1000);
    const endDate = new Date(beginDate.getTime() + 24 * 60 * 60 * 1000);
    this.taskApi.listEndBetween(beginDate, endDate).subscribe(
      response => {
        this.tasksEndInDayAfterTomorrow = response.map(param => new Task(param));
      },
      err => { }
    );
  }



}
